/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrt.utility;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author biggie
 * @version $Revision: 1.2 $ $Date: 2008/07/15 01:50:22 $
 */
public class UtilHelper {
	
	public static String toUTF8(final String str){
		if(isNullorBlank(str)){
			return str;
		}
		String retVal = str;
		try{
			retVal = new String(str.getBytes("ISO8859_1"), "UTF-8");
		} catch(Exception e){
			//log...
		}
		return retVal;
	}

	public static boolean isNullorBlank(final String str){
		boolean isNorB = false;
		if(str == null || str.length() <= 0){
			isNorB = true;
		}
		return isNorB;
	}//end isNullorBlank()

	public static String escapeHTMLTags(final String input){
		if(isNullorBlank(input))
			return input;
		StringBuffer buf = new StringBuffer();
		char ch = ' ';
		for(int i = 0; i < input.length(); i++){
			ch = input.charAt(i);
			if(ch == '<')
				buf.append("&lt;");
			else if(ch == '>')
				buf.append("&gt;");
			else if(ch == '&')
				buf.append("&amp;");
			else if(ch == '"')
				buf.append("&quot;");
			else
				buf.append(ch);
		}
		return buf.toString();
	}//end escapteHTMLTags()

	public static String convertNewlines(String input){
		input = replace(input, "\r\n", "\n");
		input = replace(input, "\n", "<BR>");
		return input;
	}
	
	public static String replace(final String mainString,final String oldString,final String newString){
		if(mainString == null)
			return null;
		int i = mainString.lastIndexOf(oldString);
		if(i < 0)
			return mainString;
		StringBuffer mainSb = new StringBuffer(mainString);
		while(i >= 0){
			mainSb.replace(i, i + oldString.length(), newString);
			i = mainString.lastIndexOf(oldString, i - 1);
		}
		return mainSb.toString();
	}

	public static String getSubStr(final String srcStr, final char ch){
		String resultStr = "";
		//the first '.' index
		int beginIndex = srcStr.indexOf(ch);
		if(0 < beginIndex){
			beginIndex++;
			//the end '.' index
			int endIndex = srcStr.indexOf(ch, beginIndex);
			//if only one '.' then get the string length
			if(0 >= endIndex){
				endIndex = srcStr.length();
			}
			//get the substring as requestFrom
			resultStr = srcStr.substring(beginIndex, endIndex);
		}
		return resultStr;
	}//end getSubStr()
	
	public static int parseInt(final String param){
		int i = -1;
		try{
			i = Integer.parseInt(param);
		} catch(Exception e){
			//i = (int)parseFloat(param);
		}
		return i;
	}//end parseInt()

	public static boolean parseBoolean(final String param){
		if(isNullorBlank(param))
			return false;
		switch(param.charAt(0)){
			case '1':
			case 'y':
			case 'Y':
			case 't':
			case 'T':
				return true;
		}
		return false;
	}//end parseBoolean()

	
	public static String getDateTime(){
		return getDateTime("yyyy-MM-dd HH:mm:ss");
	}
	/**
	 * ��ݸ�ĸ�ʽȡ��ǰʱ�䡣
	 *<p>�գ��d
	 * 
	 * Date and Time Pattern  Result  
	 * "yyyy.MM.dd G 'at' HH:mm:ss z"     2001.07.04 AD at 12:08:56 PDT  
	 * "EEE, MMM d, ''yy"                 Wed, Jul 4, '01  
	 * "h:mm a"                           12:08 PM  
	 * "hh 'o''clock' a, zzzz"            12 o'clock PM, Pacific Daylight Time  
	 * "K:mm a, z"                        0:08 PM, PDT  
	 * "yyyyy.MMMMM.dd GGG hh:mm aaa"     02001.July.04 AD 12:08 PM  
	 * "EEE, d MMM yyyy HH:mm:ss Z"       Wed, 4 Jul 2001 12:08:56 -0700  
	 * "yyMMddHHmmssZ"                    010704120856-0700  
	 * 
	 * @param pattern ָ����ʽ
	 * @return �ַ��ʾ�ĵ�ǰʱ��
	 */
	public static String getDateTime(String pattern){
		if(null == pattern || "".equals(pattern)){
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String dt = sdf.format(new Date());
		return dt;
	}

    public static String getMethod(String name,String type){
        if(name.length()>3){
            String types = name.substring(0,3);
            if(types.equals("set")||types.equals("get")){
                if(type.equals("set")){
                    return "set"+name.substring(3);
                }
                if(type.equals("get")){
                    return "get"+name.substring(3);
                }
            }
        }
        String first = name.substring(0,1);
        return type+first.toUpperCase()+name.substring(1);
    }

    public static String getFieldByMethod(String methodName){
    	if(methodName.startsWith("set") || methodName.startsWith("get")){
    		String first = methodName.substring(3,4);
    		return first.toLowerCase()+methodName.substring(4);
    	}
    	return methodName;
    }
    /**
	 * ��ݸ�ĸ�ʽ��ʽ����ʱ�䡣
	 *<p>
	 * Date and Time Pattern  Result  
	 * "yyyy.MM.dd G 'at' HH:mm:ss z"     2001.07.04 AD at 12:08:56 PDT  
	 * "EEE, MMM d, ''yy"                 Wed, Jul 4, '01  
	 * "h:mm a"                           12:08 PM  
	 * "hh 'o''clock' a, zzzz"            12 o'clock PM, Pacific Daylight Time  
	 * "K:mm a, z"                        0:08 PM, PDT  
	 * "yyyyy.MMMMM.dd GGG hh:mm aaa"     02001.July.04 AD 12:08 PM  
	 * "EEE, d MMM yyyy HH:mm:ss Z"       Wed, 4 Jul 2001 12:08:56 -0700  
	 * "yyMMddHHmmssZ"                    010704120856-0700  
	 * 
	 * @param pattern ָ����ʽ
	 * @return �ַ��ʾ�ĵ�ǰʱ��
	 */
    public static String formatDateToString(Date date,String pattern){
        if(null == pattern || "".equals(pattern)){
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
        if(date == null){
            return "";
        }
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String dt = sdf.format(date);
		return dt;
    }
    
    public static Date formatToDate(String date,String pattern) throws Exception {
        if(null == pattern || "".equals(pattern)){
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.parse(date);
    }

    public static String formatNumber(double number,String formats){
		DecimalFormat nf = new DecimalFormat(formats);
		return nf.format(number);
    }

    public static List stringToList(String[] str){
        if(str == null){
            return null;
        }
        List<String> list = new ArrayList<String>();
        for(int i=0;i<str.length;i++){
            list.add(str[i]);
        }
        return list;
    }
}
